let mobilenet ;
let video ;
let value=0;
let predictor;
let slider
let addBtn;
let trainBtn;

function modelReady(){
   console.log('model is ready')
}

function videoReady(){
   console.log('video is ready')
}

function whileTraining(loss){
    console.log(loss)
    if(loss == null){
        console.log('train done')
        predictor.predict(getResult)
    }
}
 
function getResult(error,result){
    if(error){
        console.log(error)
    }else{
        // console.log(result)
        value = result.value
        predictor.predict(getResult)
    }
}


function setup() {
    createCanvas(400, 400); 
    video = createCapture(VIDEO)
    background(0);
    mobilenet = ml5.featureExtractor('MobileNet',modelReady); //模型名称、回调函数
    predictor =  mobilenet.regression (video, videoReady);

    slider = createSlider(0,1,0.5,0.01 )
    addBtn = createButton('add images ');
    addBtn.mousePressed(function(){
        predictor.addImage(slider.value()) 
    })
 
    //学习
    trainBtn = createButton('train');
    trainBtn.mousePressed(function(){
        predictor.train(whileTraining) 
    })
}

function draw(){
    background(0)
    rectMode(CENTER)
    fill(255,0,200)
    rect(value*width,height/2,100,100)
    fill(255)
    textSize(26)
    text(value,10,height-20)
}